import request from './request'
import citydata from './citydata'

const login = function(params={}){
    return request.post("login",params,{
        public:true // 自定义的接口描述配置
    })
}

const loadNavMenus = function(){
    return request.get("menus")
}

const loadUsers = function(params={pagenum:1,pagesize:5}){
    return request.get("users",{
        params
    })
}
const addNewUser = function(user){
    return request.post("users",user)
}
const updateUser = function(uid,params={}){
    return request.put(`users/${uid}`,params)
}
const modfilyUserState = function(uid,type=false){
    return request.put(`users/${uid}/state/${type}`)
}
const deleteUserById = function(uid){
    return request.delete(`users/${uid}`)
}
const changeUserRole = function(uid,rid=""){
    return request.put(`users/${uid}/role`,{
        rid:rid
    })
}

const loadRoles = function(){
    return request.get("roles")
}
const updateRoleRights = function(roleId,rids=""){
    return request.post(`roles/${roleId}/rights`,{
        rids:rids
    })
}

const loadRights = function(type='list'){
    return request.get(`rights/${type}`)
}
const removeRightsByRid = function(roleId,rightId){
    return request.delete(`roles/${roleId}/rights/${rightId}`)
}

const loadGoods = function(params={pagenum:1,pagesize:5}){
    return request.get("goods",{
        params
    })
}

const loadCategoriesList = function(params={}){
    return request.get("categories",{
        params
    })
}
const addNewCategories = function(params){
    return request.post("categories",params)
}
const loadCategorieParams = function(cateId,sel){
    return request.get(`categories/${cateId}/attributes`,{
        params:{
            sel:sel
        }
    })
}
const addCategoriesNewParams = function(cateId,params={}){
    return request.post(`categories/${cateId}/attributes`,params)
}
const updateCategoriesNewParams = function(cateId,attrId,params={}){
    return request.put(`categories/${cateId}/attributes/${attrId}`,params)
}

const loadOrders = function(params={pagenum:1,pagesize:10}){
    return request.get("orders",{
        params
    })
}
const loadOrderDetail = function(oid){
    return request.get(`orders/${oid}`);
}

const loadCityList = function(){
    return citydata;
}

export const api = {
    login,
    loadNavMenus,
    loadUsers,addNewUser,updateUser,modfilyUserState,deleteUserById,changeUserRole,
    loadRoles,updateRoleRights,
    loadRights,removeRightsByRid,
    loadGoods,loadOrderDetail,
    loadCategoriesList,addNewCategories,loadCategorieParams,addCategoriesNewParams,updateCategoriesNewParams,
    loadOrders,
    loadCityList
}

export default {
    install:function(Vue){
        Vue.prototype.$api = api
    }
}